package com.manage.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.demo.service.CourseinfoService;

@Controller
public class CourseinfoController {
	
	@Autowired
	private CourseinfoService CourseinfoService;
	
    @RequestMapping(value="/courseinfo", method=RequestMethod.GET)
    public String courseinfo(HttpServletRequest request, Model model){
    	model.addAttribute("perm", request.getSession().getAttribute("perm"));
    	model.addAttribute("admin", request.getSession().getAttribute("admin"));
        model.addAttribute("name", request.getSession().getAttribute("name"));
		model.addAttribute("grades", CourseinfoService.getGrades());
		model.addAttribute("tch_id", CourseinfoService.getTchId());
		model.addAttribute("chinese", CourseinfoService.getChineseTchId());
		model.addAttribute("math", CourseinfoService.getMathTchId());
		model.addAttribute("english", CourseinfoService.getEnglishTchId());
		model.addAttribute("physics", CourseinfoService.getPhysicsTchId());
		model.addAttribute("chemistry", CourseinfoService.getChemistryTchId());
        return "/html/courseinfo";
    }
    
    @RequestMapping(value="/coursesearch", method=RequestMethod.GET)
    @ResponseBody
	public Map<String, List<Map<String, Object>>> search(HttpServletRequest request, @RequestParam(value = "grades", required = true) String grades, @RequestParam(value = "classes", required = true) String classes, @RequestParam(value = "tchid", required = true) String tchid, @RequestParam(value = "tchname", required = true) String tchname, @RequestParam(value = "limit", required = true) int limit, @RequestParam(value = "offset", required = true) int offset) {
		Map<String, List<Map<String, Object>>> res = new HashMap<String, List<Map<String, Object>>>();
		res.put("data", CourseinfoService.getclasslist(classes, grades, tchid, tchname, limit, offset));
		res.put("count", CourseinfoService.getclassCount(classes, grades, tchid, tchname));
		return res;
	}

	@RequestMapping(value="/updateclass", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> revise(HttpServletRequest request,@RequestParam(value = "tch_id", required = true) String tch_id, @RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", "200");
		res.put("msg", "修改成功！");
		res.put("data", CourseinfoService.updateclass(tch_id, id));
		return res;
	}
	
}
